﻿Public Class TimKiem
    Dim ctx As New QLDBDataContext
    Dim _kqtd As New dsKQTD
    Dim _clb As New dsCLB
    Dim _sb As New dsSanBong
    Dim _ct As New dsCauThu
    Public Function TKCLB(ByVal ten As String, ByVal mahlv As Integer, ByVal qg As Integer) As IQueryable(Of HUANLUYENVIEN)
        Dim ds As New dsHLV
        Dim query As IQueryable(Of HUANLUYENVIEN) = ctx.HUANLUYENVIENs

        If ten.Length > 0 Then
            query = query.Where(Function(p) p.TenHLV.Contains(ten))
        End If
        If mahlv > 0 Then
            query = query.Where(Function(p) p.MaHLV = mahlv)
        End If
        If qg > 0 Then
            query = query.Where(Function(p) p.MaQT = qg)
        End If
        query.Select(Function(p) New With {p.MaHLV, p.TenHLV})
        ' Dim querys As List(Of dsHLV) = From p In ctx.HUANLUYENVIENs
        '    Where p.TenHLV.Contains(ten)
        Return query
    End Function

    'Tim kiem lich thi dau
    Public Function TimKiemLTD(ByVal vong As Integer, ByVal madoi As Integer) As List(Of dsLTD)


        If madoi > 0 Then
            Dim query = From p In ctx.TRANDAUs
                                          Where (p.MaCLB1 = madoi Or p.MaCLB2 = madoi) And p.Vong = vong
                                          Select New dsLTD With {.Time = p.NgayTD, .Chu = _clb.LayTenDoiBong(p.MaCLB1),
                                            .Khach = _clb.LayTenDoiBong(p.MaCLB2), .San = _sb.LayTenSanBong(p.MaCLB1)}

            Return query.ToList()

        Else
            Dim query = From p In ctx.TRANDAUs
                                          Where p.Vong = vong
                                          Select New dsLTD With {.Time = p.NgayTD, .Chu = _clb.LayTenDoiBong(p.MaCLB1),
                                            .Khach = _clb.LayTenDoiBong(p.MaCLB2), .San = _sb.LayTenSanBong(p.MaCLB1)}

            Return query.ToList()
        End If


    End Function

    Public Function TimKiemKQTD(ByVal vong As Integer, ByVal madoi As Integer) As List(Of dsKQTD)


        If madoi > 0 Then
            Dim query = From p In ctx.TRANDAUs
                                          Where (p.MaCLB1 = madoi Or p.MaCLB2 = madoi) And p.Vong = vong
                                          Select New dsKQTD With {.Time = p.NgayTD, .Chu = _clb.LayTenDoiBong(p.MaCLB1),
                                                                  .TiSO = _kqtd.TiSoSTR(p.SoBanThang_CLB1, p.SoBanThang_CLB2),
                                            .Khach = _clb.LayTenDoiBong(p.MaCLB2), .San = _sb.LayTenSanBong(p.MaCLB1), .MaTran = p.MaTran}

            Return query.ToList()

        Else
            Dim query = From p In ctx.TRANDAUs
                                          Where p.Vong = vong
                                          Select New dsKQTD With {.Time = p.NgayTD, .Chu = _clb.LayTenDoiBong(p.MaCLB1),
                                                                  .TiSO = _kqtd.TiSoSTR(p.SoBanThang_CLB1, p.SoBanThang_CLB2),
                                            .Khach = _clb.LayTenDoiBong(p.MaCLB2), .San = _sb.LayTenSanBong(p.MaCLB1), .MaTran = p.MaTran}
            Return query.ToList()
        End If


    End Function

    Public Function LayDSGhiBan(ByVal maTran As Integer) As List(Of dsGhiBan)
        Dim query = From p In ctx.DANHSACHGHIBANs
                    Where p.MaTran = maTran
                    Select New dsGhiBan With {.Phut = p.ThoiDiem, .TenCT = _ct.LayTenCT(p.MaCT), .TenCLB = _clb.LayTenDoiBong(p.MaCLB), .MaTran = p.MaTran}

        Return query.ToList()
    End Function
    'lay ma hlv then clb
    Public Function LaymaHLVtheoCLB(ByVal index As Integer) As Integer
        If index > 0 Then
            Dim query = From p In ctx.CAULACBOs
                   Where p.MaCLB = index
                   Select p.MaHLV
            Return query.First()
        Else
            Return 0

        End If

    End Function


End Class
